﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Concurrent;
using RuyeeModbus.Device;
using System.Threading.Tasks;
namespace RuyeeModbus
{
    public abstract class IMaster
    {

        private List<IDevice> _DiscretionsInput;

        public List<IDevice> DiscretionsInput
        {
            get { return _DiscretionsInput; }
            set { _DiscretionsInput = value; }
        }

        private List<IDevice> _CoilsList;

        public List<IDevice> CoilsList
        {
            get { return _CoilsList; }
            set { _CoilsList = value; }
        }

        private List<IDevice> _InputRegistersList;

        public List<IDevice> InputRegistersList
        {
            get { return _InputRegistersList; }
            set { _InputRegistersList = value; }
        }

        private List<IDevice> _HoldingRegisters;

        public List<IDevice> HoldingRegisters
        {
            get { return _HoldingRegisters; }
            set { _HoldingRegisters = value; }
        }


        public bool AddDevice(IDevice Device)
        {
            switch (Device.TypeOfDevice)
            {
                case DeviceType.Coils:
                    {
                        if (CheckAddress(Device))
                        {
                            _CoilsList.Add(Device);
                        }
                        break;
                    }
            }
            return true;
        }
        bool CheckAddress(IDevice Device)
        {
           

            for (int i = 0; i < _DiscretionsInput.Count; i++)
            {
                if (_DiscretionsInput[i].Address == Device.Address)
                {
                    return false;
                }
            }
            for (int i = 0; i < _CoilsList.Count; i++)
            {
                if (_CoilsList[i].Address == Device.Address)
                {
                    return false;
                }
            }
            for (int i = 0; i < _InputRegistersList.Count; i++)
            {
                if (_InputRegistersList[i].Address == Device.Address)
                {
                    return false;
                }
            }
            for (int i = 0; i < _HoldingRegisters.Count; i++)
            {
                if (_HoldingRegisters[i].Address == Device.Address)
                {
                    return false;
                }
            }
            return true;
        }
    }
}

